package com.springboot.springbootredis.mq.stream;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StreamOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 *  
 */
@Slf4j
@Component
public class MessagePublisher {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private StreamOperations<String, String, Object> stream = this.stringRedisTemplate.opsForStream();

    public void sendMessage(String topic, String key, String value){

        Map<String, Object> record = new HashMap<>();

        record.put(key, value);

        stream.add(topic, record);

        log.info("发布消息成功 topic：{}, key:{}, value:{}", topic, key, value);
    }

}
